//
//  LTNewsChannel.m
//  wangyiNews08
//
//  Created by 123 on 16/3/12.
//  Copyright © 2016年 Tsr. All rights reserved.
//

#import "LTNewsChannel.h"

@implementation LTNewsChannel

+ (instancetype)channelWithDict:(NSDictionary *)dict {
    id obj = [[self alloc] init];
    
    [obj setValuesForKeysWithDictionary:dict];
    
    return obj;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}

+ (NSArray *)channels {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"topic_news.json" ofType:nil];
    
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    
    NSArray *channelArray = dict[@"tList"];
    
    NSMutableArray *channels = [NSMutableArray array];
    
    // 字典转模型
    [channelArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
       LTNewsChannel *channel = [LTNewsChannel channelWithDict:obj];
       
       [channels addObject:channel];
    }];
    
    return [channels sortedArrayUsingComparator:^NSComparisonResult(LTNewsChannel *obj1, LTNewsChannel *obj2) {
        return [obj1.tid compare:obj2.tid];
    }];
}

- (void)setTid:(NSString *)tid {
    _tid = tid;
    
    _URLString = [NSString stringWithFormat:@"article/headline/%@/0-140.html",_tid];
}

@end
